package me.ivps.android.weibo.othercomponent;

import java.io.File;
import java.util.List;
import java.util.concurrent.TimeUnit;

import me.ivps.android.weibo.support.debug.AppLogger;
import me.ivps.android.weibo.support.file.FileManager;
import me.ivps.android.weibo.support.utils.AppConfig;
import me.ivps.android.weibo.support.utils.GlobalContext;
import me.ivps.android.weibo.support.utils.Utility;
import android.text.TextUtils;

/**
 * 清除缓存任务
 * 
 * User: qii Date: 12-10-25
 */
public class ClearCacheTask implements Runnable {
    
    private long now = System.currentTimeMillis();
    
    @Override
    public void run() {
        // 最低优先级
        Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
        
        AppLogger.d("clear cache task start");
        
        if (Utility.isWifi(GlobalContext.getInstance())) {
            List<String> pathList = FileManager.getCachePath();
            for (String path : pathList) {
                if (!TextUtils.isEmpty(path)) {
                    handleDir(new File(path));
                }
            }
            AppLogger.d("clear cache task stop");
        }
        else {
            AppLogger
                    .d("this device dont have wifi network, clear cache task stop");
        }
    }
    
    private void clearEmptyDir(File file) {
        File[] fileArray = file.listFiles();
        if (fileArray == null) {
            return;
        }
        
        if (fileArray.length == 0) {
            if (file.delete()) {
                clearEmptyDir(file.getParentFile());
            }
        }
    }
    
    private void handleDir(File file) {
        File[] fileArray = file.listFiles();
        if (fileArray != null) {
            if (fileArray.length != 0) {
                for (File fileSI : fileArray) {
                    if (fileSI.isDirectory()) {
                        handleDir(fileSI);
                    }
                    
                    if (fileSI.isFile()) {
                        handleFile(fileSI);
                    }
                }
            }
            else {
                clearEmptyDir(file);
            }
        }
    }
    
    private void handleFile(File file) {
        long time = file.lastModified();
        long calcMills = now - time;
        long day = TimeUnit.MILLISECONDS.toDays(calcMills);
        if (day > AppConfig.SAVED_DAYS) {
            AppLogger.d(file.getAbsolutePath());
            if (file.delete()) {
                clearEmptyDir(file.getParentFile());
            }
        }
    }
}
